
import { Logger } from "../../../engine/Logger";
import ViewBase from "../../../engine/ViewBase";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubJoin extends ViewBase {

    private inputNumList: Array<number> = []
    private needNum:number = 6;

    onEnable(): void {
        super.onEnable()
        for (let i = 0; i < 10; i++) {
            let btnNum: cc.Node = this.findChild(this.node, "btnNum" + i)
            UIHelper.onAddEvent(btnNum, () => {
                if (this.inputNumList.length < this.needNum) {
                    this.inputNumList.push(i)
                    this.refresh()
                    if (this.inputNumList.length == this.needNum) {
                        this.gotoJoin()
                    }
                }
            })
        }
        // let btnBack: cc.Node = this.findChild(this.node, "btnBack")
        // UIHelper.onAddEvent(btnBack, () => {
        //     this.inputNumList = []
        //     this.refresh()
        // })
        let btnSure:cc.Node = this.findChild(this.node, "btnSure")
        UIHelper.onAddEvent(btnSure, ()=>{
            this.gotoJoin()
        })
        let btnDel: cc.Node = this.findChild(this.node, "btnDel")
        UIHelper.onAddEvent(btnDel, () => {
            this.inputNumList.pop()
            this.refresh()
        })
        this.refresh()
    }

    private refresh() {
        for (let i = 0; i < this.needNum; i++) {
            let txtNum: cc.Label = this.getChild(this.node, "txtNum" + i, cc.Label)
            if(isNaN(this.inputNumList[i])){
                txtNum.string = ""
            }else{
                txtNum.string = this.inputNumList[i] + ""
            }
        }
    }

    private gotoJoin() {
        let str: string = ""
        for (let i = 0; i < this.inputNumList.length; i++) {
            str += this.inputNumList[i]
        }
        let clubid: number = parseInt(str)
        this.closeView()
        Logger.log("gotoJoin", clubid)
        ClubSocketClient.req_club_join(clubid, () => {
        })
    }

}
